//
//  FDWebViewHtmlTool.m
//  FD
//
//  Created by xiaofang wang on 2018/12/6.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "FDWebViewHtmlTool.h"

//
static NSString *const FDTempHTMLStr = @"FDTempHTMLStr";
//轮播图模板
static NSString *const FDHomeWhirligigHTMLStr = @"FDHomeWhirligigHTMLStr";

//消息推送模板
static NSString *const FDPushMsgHTMLString = @"FDPushMsgHTMLString";


@implementation FDWebViewHtmlTool
/**
 1.将html模板字符串保存到本地
 
 @param tempHtml 模板字符串
 查看模板字符串的内容 https://s3-eu-west-1.amazonaws.com/fudiandmore/public/f3c7a213b1d342298481f147db1e31d0
 */
+ (void)fdwebViewHtmlSaveTemplateHtml:(NSString *)tempHtml {
    
    if ([NSString isEmptyString:tempHtml]) {
        FDLog(@"模板字符串为空：%@,操作无效",tempHtml);
        return;
    }
    //保存
    [[NSUserDefaults standardUserDefaults] setValue:tempHtml forKey:FDTempHTMLStr];
}


/**
 2.回本地保存的模板字符串
 
 @return 返回本地保存的模板字符串
 */
+ (NSString *)fdwebViewTempHTML {
    
    NSString *templateHTMLStr = [[NSUserDefaults standardUserDefaults] valueForKey:FDTempHTMLStr];
    
//    FDLog(@"本地保存的模板字符串:%@",templateHTMLStr);
    return templateHTMLStr;
}

#pragma mark - 12-30 轮播图模板字符串保存本地,从本地获取
+ (void)fdwebViewHtmlSaveHomeWhirligigTemplateHtml:(NSString *)tempHtml {
    
    if ([NSString isEmptyString:tempHtml]) {
        FDLog(@"模板字符串为空：%@,操作无效",tempHtml);
        return;
    }
    //保存
    [[NSUserDefaults standardUserDefaults] setValue:tempHtml forKey:FDHomeWhirligigHTMLStr];
}

+ (NSString *)fdHomeWhirligigWebViewTempHTML {
    NSString *templateHTMLStr = [[NSUserDefaults standardUserDefaults] valueForKey:FDHomeWhirligigHTMLStr];
    
    //    FDLog(@"本地保存的模板字符串:%@",templateHTMLStr);
    return templateHTMLStr;
}



#pragma mark - 1-5 推送消息html模板保存本地.从本地加载
/***保存推送消息html模板*/
+ (void)fdSavePushMsgHtmlTemplate:(NSString *)temphtml {
    if ([NSString isEmptyString:temphtml]) {
        FDLog(@"模板字符串为空：%@,操作无效",temphtml);
        return;
    }
    //保存
    [[NSUserDefaults standardUserDefaults] setValue:temphtml forKey:FDPushMsgHTMLString];
}

/**从本地获取推送消息html模板*/
+ (NSString *)fdPushMsgHtmlTemplate {
    
    NSString *pushmsghtmlTemplate =  [[NSUserDefaults standardUserDefaults] valueForKey:FDPushMsgHTMLString];
    
    return pushmsghtmlTemplate;
}




#pragma mark - 替换
/**
 3.替换模板 html 字符串中的 ${content}
 
 @param htmlStr 要替换的html字符串
 */
+ (NSString *)fdwebViewReplaceTemplateHtmlMainDivContentWithHtmlStr:(NSString *)htmlStr {

    //拿到模板字符串
    NSString *templatehtmlStr = [self fdwebViewTempHTML];
    if ([NSString isEmptyString:templatehtmlStr]) {
        FDLog(@"模板字符串不存在操作无效:%@",templatehtmlStr);
        return @"";
    }
    
    if ([NSString isEmptyString:htmlStr]) {
        FDLog(@"内容字符串不存在操作无效:%@",htmlStr);
        return @""; //nil; 会奔
    }
    
    
    NSString *resultStr =  [templatehtmlStr stringByReplacingOccurrencesOfString:@"${content}" withString:htmlStr];
    
    return resultStr;
}

#pragma mark - private

@end
